# LangGraph Mastery: From Foundations to Production

[![LangGraph](https://img.shields.io/badge/LangGraph-0.2+-blue)](https://langchain-ai.github.io/langgraph/)
[![Python](https://img.shields.io/badge/Python-3.9+-green?logo=python)](https://python.org)
[![LangSmith](https://img.shields.io/badge/LangSmith-Enabled-purple)](https://smith.langchain.com)
[![License](https://img.shields.io/badge/License-MIT-yellow)](LICENSE)

> **A complete practical guide to building AI agents, multi-agent orchestration, and production systems with LangGraph**

📕 **Includes a 100+ page PDF book** with 20 chapters, 22 Python programs, and real-world examples.

---

## 📋 Table of Contents

- [Book PDF Download](#-book-pdf)
- [Quick Start](#-quick-start)
- [Chapter Guide](#-chapter-guide)
- [Project Structure](#-project-structure)
- [Key Concepts](#-key-concepts)
- [Architecture Diagrams](#-architecture-diagrams)

---

## 📕 Book PDF

The complete **LangGraph Mastery** book is available as a professionally formatted PDF:

**📖 [LangGraph_Mastery_Complete_Book.pdf](LangGraph_Mastery_Complete_Book.pdf)**

20 Chapters | 22 Python Programs | Real-World Projects

---

## 🚀 Quick Start

### Step 1: Clone and Setup

```bash
git clone https://github.com/yourusername/langgraph-mastery.git
cd langgraph-mastery

python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install -r requirements.txt
```

### Step 2: Configure API Keys

```bash
cp .env.example .env
# Edit .env with your OpenAI/Anthropic API keys
```

### Step 3: Run Your First Graph

```bash
python src/01_hello_langgraph.py
```

### Step 4: Explore Chapters in Order

```bash
python src/02_state_basics.py
python src/04_conditional_routing.py
python src/10_multi_agent.py
python src/20_capstone_project.py
```

---

## 📚 Chapter Guide

### Part I: Foundations

| Chapter | File | Topic | Key Concepts |
|---------|------|-------|--------------|
| 1 | - | Introduction to LangGraph | Graphs vs Chains, why LangGraph |
| 2 | - | Environment Setup | Python, API keys, LangSmith |
| 3 | `02_state_basics.py` | **Understanding State** | ★ TypedDict, ★ Pydantic, ★ Annotated, ★ Reducers |
| 4 | `01_hello_langgraph.py` | **Your First Graph** | ★ StateGraph, ★ compile(), ★ invoke() |
| 5 | `03_nodes_and_edges.py` | Nodes: Building Blocks | Node patterns, parallel execution |
| 6 | `04_conditional_routing.py` | **Conditional Routing** | ★ add_conditional_edges, ★ Router functions, ★ Cycles |

### Part II: Core Capabilities

| Chapter | File | Topic | Key Concepts |
|---------|------|-------|--------------|
| 7 | `05_llm_integration.py` | LangChain + LLMs | ChatOpenAI, ChatAnthropic, MessagesState |
| 8 | `06_tool_calling.py` | **Tools & Tool Calling** | ★ @tool, ★ bind_tools, ★ ToolNode, ★ tools_condition |
| 9 | `07_memory_persistence.py` | **Memory & Persistence** | ★ MemorySaver, ★ thread_id, ★ Checkpointers |
| 10 | `08_human_in_loop.py` | Human-in-the-Loop | ★ interrupt_before, ★ update_state |

### Part III: Advanced Patterns

| Chapter | File | Topic | Key Concepts |
|---------|------|-------|--------------|
| 11 | `09_subgraphs.py` | Subgraphs | Modular architecture, composability |
| 12 | `10_multi_agent.py` | **Multi-Agent Orchestration** | ★ Supervisor pattern, ★ Specialist agents |
| 13 | `11_rag_agent.py` | **RAG with LangGraph** | Retrieval agents, retry loops |
| 14 | `12_vector_search.py` | Vector Databases | ChromaDB, embeddings, semantic search |

### Part IV: Production Systems

| Chapter | File | Topic | Key Concepts |
|---------|------|-------|--------------|
| 15 | `13_chatbot.py` | Production Chatbot | Intent detection, multi-handler routing |
| 16 | `14_langsmith_debug.py` | **LangSmith Monitoring** | Tracing, debugging, evaluation |
| 17 | `15_mcp_agents.py` | **MCP Protocol** | ★ Agent-to-agent communication |
| 18 | `16_streaming.py` | Streaming & Advanced | stream_mode, map-reduce, error handling |
| 19 | `19_deployment.py` | Production Deployment | FastAPI, scaling, checklist |
| 20 | `20_capstone_project.py` | **Capstone Project** | Full-stack AI agent system |

---

## 📁 Project Structure

```
langgraph-mastery/
├── 📕 LangGraph_Mastery_Complete_Book.pdf   # The complete book
├── 📖 README.md
├── 📦 requirements.txt
├── ⚙️  .env.example
│
├── src/                          # Numbered source files
│   ├── 01_hello_langgraph.py     # First graph
│   ├── 02_state_basics.py        # State concepts
│   ├── 03_nodes_and_edges.py     # Node patterns
│   ├── 04_conditional_routing.py # Dynamic routing
│   ├── 05_llm_integration.py     # LLM connections
│   ├── 06_tool_calling.py        # Tool usage
│   ├── 07_memory_persistence.py  # Checkpointers
│   ├── 08_human_in_loop.py       # Human oversight
│   ├── 09_subgraphs.py           # Modularity
│   ├── 10_multi_agent.py         # Multi-agent
│   ├── 11_rag_agent.py           # RAG pipeline
│   ├── 12_vector_search.py       # Vector DB
│   ├── 13_chatbot.py             # Chatbot
│   ├── 14_langsmith_debug.py     # Monitoring
│   ├── 15_mcp_agents.py          # MCP protocol
│   ├── 16_streaming.py           # Streaming
│   ├── 17_map_reduce.py          # Parallel
│   ├── 18_error_handling.py      # Error patterns
│   ├── 19_deployment.py          # FastAPI
│   ├── 20_capstone_project.py    # Full system
│   └── utils/
│       ├── __init__.py
│       └── llm_config.py         # LLM utilities
│
├── data/                          # Sample data
├── tests/                         # Unit tests
└── docs/                          # Additional docs
```

---

## 🔑 Key Concepts

### ★ State (The Heart of LangGraph)

```python
from typing import TypedDict, Annotated
from operator import add

class MyState(TypedDict):
    messages: Annotated[list, add]  # APPEND reducer
    result: str                      # REPLACE (default)
```

### ★ Graph Construction Pattern

```python
from langgraph.graph import StateGraph, START, END

graph = StateGraph(MyState)           # 1. Create graph
graph.add_node("name", function)      # 2. Add nodes
graph.add_edge(START, "name")         # 3. Add edges
graph.add_conditional_edges(...)      # 4. Add routing
app = graph.compile()                 # 5. Compile
result = app.invoke(initial_state)    # 6. Run
```

### ★ The Agent Loop

```
START → Agent → Should Continue? → YES → Tools → Agent (loop)
                                 → NO  → END
```

### ★ Key Terms Quick Reference

| Term | Definition |
|------|-----------|
| **StateGraph** | The graph builder class |
| **TypedDict** | State schema definition |
| **Pydantic** | State with runtime validation |
| **Annotated** | Adds reducer to state field |
| **Reducer** | Controls how state updates merge |
| **Node** | Function that processes state |
| **Edge** | Connection between nodes |
| **Checkpointer** | Saves state for memory/recovery |
| **thread_id** | Unique conversation identifier |
| **ToolNode** | Executes LLM tool calls |
| **MCP** | Model Context Protocol |
| **LangSmith** | Monitoring and debugging platform |

---

## 🏗️ Architecture Diagrams

### Simple Graph
```
START → [Classify] → [Process] → [Output] → END
```

### Conditional Routing
```
START → [Classify] ──→ [Handler A] ──→ END
                  ├──→ [Handler B] ──→ END
                  └──→ [Handler C] ──→ END
```

### Agent Loop (with Tools)
```
START → [Agent] ←──────────── [Tools]
           │                      ↑
           ├── has tool calls? ───┘
           │
           └── no tools → END
```

### Multi-Agent (Supervisor)
```
START → [Supervisor] ←── [Researcher]
              │      ←── [Writer]
              │      ←── [Reviewer]
              └── FINISH → END
```

---

## 📄 License

MIT License

---

**Happy Agent Building! 🤖**
